﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Web.UI.WebControls;
using MyWeb.Business;
using MyWeb.Common;

namespace MyWeb.Modules.News
{
    public partial class NewsDefault : System.Web.UI.Page
    {
        private int currentpage { get { return ViewState["currentpage"] != null ? int.Parse(ViewState["currentpage"].ToString()) : 1; } set { ViewState["currentpage"] = value; } }
        private int pageCount { get { return ViewState["pageCount"] != null ? int.Parse(ViewState["pageCount"].ToString()) : 1; } set { ViewState["pageCount"] = value; } }
        private int pagesize = 2;
        string Where = "";
        protected void Page_Load(object sender, EventArgs e)
        {
            if(!IsPostBack)
            {
                PreLoad();
                LoadMetaConfig();
            }
        }
        private void PreLoad()
        {
            var listnew = NewsService.News_Paging(currentpage.ToString(), pagesize.ToString(), "*", LoadWhere(), "[Date] desc");
            Show(listnew);
            BindPaging();
        }
        private void Show(List<Data.News> list)
        {
            var s = "";
            s += "<div class=\"News\"><div class=\"nBox\">";
            s += "<div class=\"NewsTitle\">Tin tức</div>";
            //var list = NewsService.News_GetByTop("10", "Active = '1'", "[Date] desc");
            if (list.Count > 0)
            {
                foreach (var t in list)
                {
                    var link = "/tin-tuc/" + t.Tag + "/" + t.GroupNewId + "/" + t.Id + ".html";
                    s += "<div class=\"nContent clearn\">";
                    //
                    s += "<div class=\"nTitle\"><h1><a href=\"" + link + "\">" + t.Name + "</a></h1></div>";
                    //
                    s += "<div class=\"nDetail\">";
                    s += "<a href=\"" + link + "\"><img src='" + t.Image + "' alt='" + t.Name + "'/></a>";
                    s += "<p>" + StringClass.SubText(t.Content, 80) + "</p>";
                    s += "</div>";
                    //
                    s += "</div>";
                }
            }
            s += "</div></div>";
            ltrnews.Text = s;
        }
        #region Load where
        private string LoadWhere()
        {
            Where = "Active = '1'";
            int recordCount = Int32.Parse(DataBindSQL.ExecuteScalar("Select Count(*) from [News] Where " + Where));
            pageCount = (recordCount % pagesize == 0) ? recordCount / pagesize : recordCount / pagesize + 1;
            return Where;
        }
        #endregion
        #region Paging
        private void BindPaging()
        {
            int icurPage = currentpage;
            int ipCount = pageCount;
            first.Visible = false; prev.Visible = false; next.Visible = false; last.Visible = false; pNum.Visible = false;
            if (ipCount > 1)
            {
                pNum.Visible = true;
                int PageShow = 5;
                int FromPage;
                int ToPage;
                DataTable dt = new DataTable();
                dt.Columns.Add("PageIndex");
                dt.Columns.Add("PageText");
                FromPage = icurPage > PageShow ? icurPage - PageShow : 1;
                ToPage = (ipCount - icurPage > PageShow) ? icurPage + PageShow : ipCount;
                ltrPrivious.Text = "";
                ltrNext.Text = "";
                if (icurPage > 1)
                {
                    first.Visible = true; prev.Visible = true;
                }
                if (icurPage < ipCount)
                {
                    next.Visible = true; last.Visible = true;
                }
                if (icurPage > PageShow + 1) ltrPrivious.Text = "<span class=\"separator\">...</span>";
                if (icurPage - 10 > 0) dt.Rows.Add(icurPage - 9, icurPage - 10);
                for (int i = FromPage; i <= ToPage; i++)
                {
                    DataRow dr = dt.NewRow();
                    dr[0] = i;
                    dr[1] = i;
                    dt.Rows.Add(dr);
                }
                if (icurPage + 10 <= ipCount) dt.Rows.Add(icurPage + 9, icurPage + 10);
                pNum.DataSource = dt;
                pNum.DataBind();
                if (icurPage + (PageShow + 2) < ipCount) ltrNext.Text = string.Format("<span class=\"separator\">...</span>");
            }
        }

        public void Page_Number_Click(object sender, EventArgs e)
        {
            string curPage = ((LinkButton)sender).CommandArgument;
            currentpage = int.Parse(curPage);
            PreLoad();
        }
        public void Page_Button_Click(object sender, EventArgs e)
        {
            if (((LinkButton)sender).ID == "prev")
            {
                if (currentpage > 1) currentpage = currentpage - 1;
            }
            else if (((LinkButton)sender).ID == "next")
            {
                if (currentpage < pageCount) currentpage = currentpage + 1;
            }
            else if (((LinkButton)sender).ID == "last")
            {
                currentpage = pageCount;
            }

            else if (((LinkButton)sender).ID == "first")
            {
                currentpage = 1;
            }
            PreLoad();
        }

        protected void pNum_ItemCommand(object source, DataListCommandEventArgs e)
        {
            if (e.CommandName.Equals("page"))
            {
                currentpage = Convert.ToInt32(e.CommandArgument.ToString());
                PreLoad();
            }
        }

        protected void pNum_ItemDataBound(object sender, DataListItemEventArgs e)
        {
            LinkButton lnkPage = (LinkButton)e.Item.FindControl("btn");
            if (lnkPage.CommandArgument.ToString() == currentpage.ToString())
            {
                lnkPage.Enabled = false;
                lnkPage.CssClass = "selected";
            }
        }
        #endregion
        #region[LoadMetaConfig]
        void LoadMetaConfig()
        {
            Page.Title = "Tin tức";
            Page.MetaDescription = GlobalClass.Description;
            Page.MetaKeywords = GlobalClass.Keyword;
        }
        #endregion
    }
}